<?
$date_formats = array(
	'd/m/Y' => 'dd/mm/yy',
	'Y/m/d' => 'yy/mm/dd',
);
$date_format = $date_formats[$langs[$lang]->date_format];
?>

<script src="<?= $url_styles ?>js/plugins/formselements/jquery.maskedinput-1.3.min.js"></script>
<script src="<?= $url_styles ?>js/plugins/formselements/ui.spinner.min.js"></script>
<script src="<?= $url_styles ?>js/plugins/gallery/jquery.colorbox-min.js"></script>
<script>

    //	$('.form-horizontal [name="price"]').spinner({ min: 0,numberFormat: 'n',step: 0.01});
    //	$('.form-horizontal [name="sale_price"]').spinner({ min: 0,numberFormat: 'n'});
    $('.form-horizontal [name="start"]').datepicker();
    $('.form-horizontal [name="end"]').datepicker();
    $('.form-horizontal [name="start"]').datepicker('option','dateFormat','<?= $date_format ?>');
    $('.form-horizontal [name="end"]').datepicker('option','dateFormat','<?= $date_format ?>');
    var image_index=0;
    $('#add_image').live('click',function(){
        image_index++;
        $('#images').append('<div><input type="file" name="images_'+image_index+'" class="input-xlarges"/>\n\
							<a href="javascript:;" class="delete_image"><?= lang('delete') ?></a></div>');
        return false;
    });
    $('.delete_image').live('click',function(){
        $(this).parent().fadeOut('fast',function(){$(this).remove()});
    });
    $('#tab4_check').click(function(){
        $('.form-horizontal [name="catalogues[]"]').attr('checked',this.checked);
    })
    $('#tab5_check').click(function(){
        $('.form-horizontal [name="locations[]"]').attr('checked',this.checked);
    })
    $(document).ready(function(){
        var product = <?= json_encode($product) ?>;
        $('.form-horizontal .product_image').attr('src',product.thumb);
        $('.form-horizontal .product_image').attr('style','max-width:120px;max-height:120px');
		for(var i in product){
			var e = $('.form-horizontal [name="'+i+'"]')
			switch(e.attr('type'))
			{
				case 'file':
					break;
				case 'checkbox':
					if(product[i]==1) e.attr('checked',true);
					break;
				default:
					e.val(product[i]);				
			}
		}
		
        for(var i in product.info){
            var info = product.info[i];
			for(var j in info){
				$('.form-horizontal [name="info['+info.lang_id+']['+j+']"]"').val(info[j]);
			}
        }
        for(var i in product.catalogues){
            var cat_id = product.catalogues[i].cat_id;
            $('.form-horizontal [name="catalogues[]"][value="'+cat_id+'"]').attr('checked',true);
        }
        for(var i in product.locations){
            var location_id = product.locations[i].location_id;
            $('.form-horizontal [name="locations[]"][value="'+location_id+'"]').attr('checked',true);
        }
        var img_gallery = '';
        for(var i in product.images)
        {
            var img=product.images[i];
            img_gallery+='<li>'+
				'<img src="'+img.thumb+'" alt="placeholder">'+
				'<div>'+
				'<ul style="width:67px">'+
				'<li class="edit"><a href="'+img.path+'"><?= lang('view') ?></a></li>'+
				'<li class="delete" data="'+img.id+'"><a href="#"><?= lang('delete') ?></a></li>'+
				'</ul>'+
				'</div>'+
				'</li>';
        }
        $('#basicGallery').html(img_gallery);
        var errors = <?= json_encode($errors) ?>;
        for(var i in errors)
        {
            if(i=='id' || i=='catalogues' || i=='locations')
            {
				$('.form-horizontal').children(':first').before('<div class="alert alert-error">'+
                        '<button data-dismiss="alert" class="close" type="button">×</button>'+
                        errors[i]+
                      '</div>');
            }
            else if(errors[i]!=''){
                $('.form-horizontal [name="'+i+'"]').after('<span class="help-inline">'+errors[i]+'</span>');
                $('.form-horizontal [name="'+i+'"]').parent().parent().addClass('error');
            }

        }

    });
    jQuery(function($) {

        /* --->> 1 - Basic gallery options show/hide --------------*/
        $("#basicGallery > li").hoverIntent(
		function () {
			$(this).find("div").fadeIn("fast");
		},
		function () {
			$(this).find("div").fadeOut("fast");
		}
	);

        /* --->> 2 - Basic gallery setup --------------*/
        $("#basicGallery .edit a").colorbox({rel:'gal'});

        /* --->> 3 - Delete images --------------*/
        $("#basicGallery .delete").click(function(){
            if(!confirm('<?= lang('delete_confirm') ?>')) return false;
            $.ajax({
                url: '<?= url_create('admin', 'product', 'delete_image') ?>',
                type: 'POST',
                dataType: 'json',
                data: {id: $(this).attr('data')},
                success: function(o)
                {
                    if(o.state=='success')
                        $('#basicGallery li [data="'+ o.id+'"]').fadeOut().remove();
                    else
                        $.jGrowl(o.message, {
                            header: o.title,
                            theme:"notification_styled_"+o.state
                        });
                }

            })
            return false;
        });

    });
</script>
